import { NUploadTrigger } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";
export type UploadTriggerFactoryConstructorType = {
  attrs?: HTMLAttributes;
  props?: { abstract: boolean };
  defaultSlot?: (options: {
    handleClick: () => void;
    handleDragOver: (e: DragEvent) => void;
    handleDragEnter: (e: DragEvent) => void;
    handleDragLeave: (e: DragEvent) => void;
    handleDrop: (e: DragEvent) => void;
  }) => BaseComponentType;
};
export class UploadTriggerFactory {
  private attrs: HTMLAttributes = {};
  private props: { abstract: boolean } = { abstract: false };
  private defalutSlot: (options: {
    handleClick: () => void;
    handleDragOver: (e: DragEvent) => void;
    handleDragEnter: (e: DragEvent) => void;
    handleDragLeave: (e: DragEvent) => void;
    handleDrop: (e: DragEvent) => void;
  }) => BaseComponentType = () => null;

  constructor(data?: UploadTriggerFactoryConstructorType) {
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.props) this.setProps(data.props);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
  }
  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: { abstract: boolean }) {
    this.props = props;
    return this;
  }

  setDefault(
    component: (options: {
      handleClick: () => void;
      handleDragOver: (e: DragEvent) => void;
      handleDragEnter: (e: DragEvent) => void;
      handleDragLeave: (e: DragEvent) => void;
      handleDrop: (e: DragEvent) => void;
    }) => BaseComponentType
  ) {
    this.defalutSlot = component;
    return this;
  }

  create() {
    return (
      <NUploadTrigger {...this.attrs} {...this.props}>
        {{
          default: (options: {
            handleClick: () => void;
            handleDragOver: (e: DragEvent) => void;
            handleDragEnter: (e: DragEvent) => void;
            handleDragLeave: (e: DragEvent) => void;
            handleDrop: (e: DragEvent) => void;
          }) => getSlotsDom(this.defalutSlot(options)),
        }}
      </NUploadTrigger>
    );
  }
}
